package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.constants.BackstageCategoryConstants;
import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.exception.SupermarketException;
import cn.outsourcing.supermarket.converter.HiolabsCategoryConverter;
import cn.outsourcing.supermarket.dto.OnlyIdPayloadDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageCategoryStatusDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageCategoryStoreDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageUpdateSortDTO;
import cn.outsourcing.supermarket.mapper.HiolabsCategoryMapper;
import cn.outsourcing.supermarket.po.HiolabsCategory;
import cn.outsourcing.supermarket.service.BackstageCategoryService;
import cn.outsourcing.supermarket.vo.backstage.BackstageCategoryInfoVO;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsCategoryVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author gzkemays
 * @since 2021/11/4 14:46
 */
@Service
public class BackstageCategoryServiceImpl implements BackstageCategoryService {
  @Resource HiolabsCategoryMapper categoryMapper;

  @Override
  public List<BackstageCategoryInfoVO> getCategory() {
    return HiolabsCategoryConverter.INSTANCE.convertToVoInfoList(
        categoryMapper.selectList(
            Wrappers.<HiolabsCategory>lambdaQuery().orderByAsc(HiolabsCategory::getSortOrder)));
  }

  @Override
  public BackstageCategoryInfoVO store(BackstageCategoryStoreDTO dto) {
    HiolabsCategory category = HiolabsCategoryConverter.INSTANCE.convertToPoByCategoryStoreDto(dto);
    if (Objects.equals(category.getId(), 0)) {
      category.setId(null);
      categoryMapper.insert(category);
    } else {
      categoryMapper.updateById(category);
    }
    return HiolabsCategoryConverter.INSTANCE.convertToVoInfo(category);
  }

  @Override
  public HiolabsCategoryVO info(OnlyIdPayloadDTO dto) {
    return HiolabsCategoryConverter.INSTANCE.convertToVo(categoryMapper.selectById(dto.getId()));
  }

  @Override
  public String updateStatus(BackstageCategoryStatusDTO dto, Integer mode) {
    HiolabsCategory category = categoryMapper.selectById(dto.getId());
    if (mode.equals(BackstageCategoryConstants.STATUS_SHOW)) {
      category.setIsShow(dto.getStatus());
    }
    if (mode.equals(BackstageCategoryConstants.STATUS_CHANNEL)) {
      category.setIsChannel(dto.getStatus());
    }
    if (mode.equals(BackstageCategoryConstants.STATUS_CATEGORY)) {
      category.setIsCategory(dto.getStatus());
    }
    if (categoryMapper.updateById(category) > 0) {
      return "OK";
    }
    throw new SupermarketException(CommonConstants.SQL_FAIL_CODE, "更新失败");
  }

  @Override
  public Integer updateSort(BackstageUpdateSortDTO dto) {
    HiolabsCategory category = new HiolabsCategory();
    category.setSortOrder(dto.getSort());
    category.setId(dto.getId());
    return categoryMapper.updateById(category);
  }

  @Override
  public Integer destroy(OnlyIdPayloadDTO dto) {
    return categoryMapper.deleteById(dto.getId());
  }
}
